import { getRule } from '../src/rule';
import { IParams } from '../src/params';
import { expect } from 'chai';
import sinon from 'sinon';

describe('getRule', () => {
    let sandbox: sinon.SinonSandbox;
    let checkCustomRuleDirStub: sinon.SinonStub;


    beforeEach(() => {
        sandbox = sinon.createSandbox();
        checkCustomRuleDirStub = sandbox.stub(require('../src/utils'), 'checkCustomRuleDir')

    });

    afterEach(() => {
        sandbox.restore();
    });

    it('should return custom rule files when useCustomRule is true', () => {
        const customRuleDir = 'test/testDir';
        checkCustomRuleDirStub.returns(customRuleDir)
        const params = {
            useCustomRule: true,
            customRuleDir,
            pluginDir: ''
        } as IParams;

        const result = getRule(params);

        expect(result).to.equal('test/testDir/1.xml,test/testDir/2.xml');

    });

    it('should throw error when useCustomRule is true but no rule found', () => {
        const customRuleDir = 'test/testDir2';
        checkCustomRuleDirStub.returns(customRuleDir)
        const params = {
            useCustomRule: true,
            customRuleDir,
            pluginDir: ''
        } as IParams;

        try {
            const result = getRule(params);
            expect.fail('should throw an error')
        }catch (e: any){
            expect(e.message).to.equal(`No xml suffixed file found in custom rule dir ${customRuleDir}.`);
        }

    });



    it('should return default rule files when useCustomRule is false', () => {
        const params = {
            useCustomRule: false,
            pluginDir: '/root/plugin',
            ruleSet: 'ali-comment.xml,ali-concurrent.xml,ali-constant.xml,ali-exception.xml,ali-flowcontrol.xml,ali-naming.xml,ali-oop.xml,ali-orm.xml,ali-other.xml,ali-set.xml'
        } as IParams;

        const result = getRule(params);

        expect(result).to.equal('/root/plugin/p3c/rule/java/ali-comment.xml,/root/plugin/p3c/rule/java/ali-concurrent.xml,/root/plugin/p3c/rule/java/ali-constant.xml,/root/plugin/p3c/rule/java/ali-exception.xml,/root/plugin/p3c/rule/java/ali-flowcontrol.xml,/root/plugin/p3c/rule/java/ali-naming.xml,/root/plugin/p3c/rule/java/ali-oop.xml,/root/plugin/p3c/rule/java/ali-orm.xml,/root/plugin/p3c/rule/java/ali-other.xml,/root/plugin/p3c/rule/java/ali-set.xml');
    });

})